package com.mingtu.dzpatrol.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Switch;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPStaticUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnConfirmListener;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.mingtu.dzpatrol.R;
import com.mingtu.dzpatrol.base.BaseActivity;
import com.mingtu.dzpatrol.callback.StringDialogCallback;
import com.mingtu.dzpatrol.utils.ActivityUtil;
import com.mingtu.dzpatrol.utils.Constant;
import com.mingtu.dzpatrol.utils.IntentUtils;
import com.mingtu.dzpatrol.utils.SPTools;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class InfoActivity extends BaseActivity {

    @BindView(R.id.tv_user_name)
    TextView tvUserName;
    @BindView(R.id.tv_mobile)
    TextView tvMobile;
    @BindView(R.id.tv_dept_name)
    TextView tvDeptName;
    @BindView(R.id.tv_role_name)
    TextView tvRoleName;
    @BindView(R.id.button_exit)
    Button buttonExit;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_info;
    }

    @Override
    protected void initData() {
        setModuleTitle("基本信息");
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void getData() {
        String name = getIntent().getStringExtra("name");
        String mobile = getIntent().getStringExtra("mobile");
        String roleName = getIntent().getStringExtra("roleName");
        String deptName = getIntent().getStringExtra("deptName");

        if (!StringUtils.isEmpty(name)) {
            tvUserName.setText(name);
        }
        if (!StringUtils.isEmpty(mobile)) {
            tvMobile.setText(mobile);
        }
        if (!StringUtils.isEmpty(roleName)) {
            tvRoleName.setText(roleName);
        }
        if (!StringUtils.isEmpty(deptName)) {
            tvDeptName.setText(deptName);
        }


    }


    @OnClick({R.id.button_exit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.button_exit:
                new XPopup.Builder(ActivityUtils.getTopActivity())
                        .isDestroyOnDismiss(true)
                        .dismissOnTouchOutside(true)
                        .hasShadowBg(true)
                        .asConfirm(context.getResources().getString(R.string.text_tip), "是否退出登录？",
                                context.getResources().getString(R.string.text_cancle), context.getResources().getString(R.string.text_sure),
                                new OnConfirmListener() {
                                    @Override
                                    public void onConfirm() {
                                        logout();
                                    }
                                }, null, false)
                        .show();
                break;
        }
    }

    /**
     * 退出登录
     */
    private void logout() {
        if (netWorkState) {
            OkGo.<String>post(Constant.getInstance().POST_LOGOUT)
                    .tag(this)
                    .upJson("")
                    .execute(new StringDialogCallback(ActivityUtil.getTopActivity()) {
                        @Override
                        public void onSuccess(Response<String> response) {
                            String result = response.body();
                            if (!StringUtils.isEmpty(result)) {
                                JSONObject jsonObject = JSON.parseObject(result);
                                String msg = jsonObject.getString("msg");
                                if (msg.equals(Constant.RESULT_SUCCESS)) {
                                    SPStaticUtils.remove(SPTools.token);

                                    ToastUtils.showLong("退出成功！");

                                    IntentUtils.getInstance().readyGo(ActivityUtil.getTopActivity(), LoginActivity.class);
                                    ActivityUtil.clearActivity();


                                } else {
                                    ToastUtils.showLong("退出失败！");
                                }
                            }
                        }

                        @Override
                        public void onError(Response<String> response) {
                            String result = response.body();
                            LogUtils.e(getClass().getSimpleName(), "onError==" + result);

                        }
                    });
        } else {
            SPStaticUtils.remove(SPTools.token);
            IntentUtils.getInstance().readyGo(ActivityUtil.getTopActivity(), LoginActivity.class);
            ActivityUtil.clearActivity();
        }
    }
}